import request from '../utils/request';
import { API } from '../utils/config';

/**
 * 会员等级接口
 */
export interface MemberLevel {
  objectId?: string;
  level: string;
  name: string;
  condition: string;
  benefits: string;
  gifts: string;
  memberCount: number;
  createdAt?: string;
  updatedAt?: string;
}

/**
 * 获取会员等级列表
 * @param params 查询参数
 */
export const getMemberLevels = (params?: any) => {
  return request({
    url: `${API.CLASSES}/MemberLevel`,
    method: 'GET',
    params: {
      order: 'level',
      limit: 1000,
      count: 1,
      ...params
    }
  });
};

/**
 * 创建会员等级
 * @param data 会员等级数据
 */
export const createMemberLevel = (data: Omit<MemberLevel, 'objectId' | 'createdAt' | 'updatedAt'>) => {
  return request({
    url: `${API.CLASSES}/MemberLevel`,
    method: 'POST',
    data
  });
};

/**
 * 更新会员等级
 * @param id 会员等级ID
 * @param data 会员等级数据
 */
export const updateMemberLevel = (id: string, data: Partial<MemberLevel>) => {
  return request({
    url: `${API.CLASSES}/MemberLevel/${id}`,
    method: 'PUT',
    data
  });
};

/**
 * 删除会员等级
 * @param id 会员等级ID
 */
export const deleteMemberLevel = (id: string) => {
  return request({
    url: `${API.CLASSES}/MemberLevel/${id}`,
    method: 'DELETE'
  });
};

/**
 * 获取会员等级详情
 * @param id 会员等级ID
 */
export const getMemberLevelDetail = (id: string) => {
  return request({
    url: `${API.CLASSES}/MemberLevel/${id}`,
    method: 'GET'
  });
};

/**
 * 客户接口
 */
export interface Customer {
  objectId?: string;
  name: string;
  phone: string;
  email?: string;
  memberLevel: string;
  totalAmount: number;
  orderCount: number;
  lastOrderTime?: string;
  registerTime: string;
  createdAt?: string;
  updatedAt?: string;
}

/**
 * 获取客户列表
 * @param params 查询参数
 */
export const getCustomerList = (params?: any) => {
  return request({
    url: `${API.CLASSES}/Customer`,
    method: 'GET',
    params: {
      order: '-createdAt',
      limit: params?.limit || 10,
      skip: params?.skip || 0,
      count: 1,
      ...params
    }
  });
};

/**
 * 创建客户
 * @param data 客户数据
 */
export const createCustomer = (data: Omit<Customer, 'objectId' | 'createdAt' | 'updatedAt'>) => {
  return request({
    url: `${API.CLASSES}/Customer`,
    method: 'POST',
    data
  });
};

/**
 * 更新客户信息
 * @param id 客户ID
 * @param data 客户数据
 */
export const updateCustomer = (id: string, data: Partial<Customer>) => {
  return request({
    url: `${API.CLASSES}/Customer/${id}`,
    method: 'PUT',
    data
  });
};

/**
 * 删除客户
 * @param id 客户ID
 */
export const deleteCustomer = (id: string) => {
  return request({
    url: `${API.CLASSES}/Customer/${id}`,
    method: 'DELETE'
  });
};

/**
 * 客户分组接口
 */
export interface CustomerGroup {
  objectId?: string;
  name: string;
  description: string;
  count: number;
  createdAt?: string;
  updatedAt?: string;
}

/**
 * 获取客户分组列表
 */
export const getCustomerGroups = (params?: any) => {
  return request({
    url: `${API.CLASSES}/CustomerGroup`,
    method: 'GET',
    params: {
      order: '-createdAt',
      limit: 1000,
      count: 1,
      ...params
    }
  });
};

/**
 * 创建客户分组
 * @param data 分组数据
 */
export const createCustomerGroup = (data: Omit<CustomerGroup, 'objectId' | 'createdAt' | 'updatedAt'>) => {
  return request({
    url: `${API.CLASSES}/CustomerGroup`,
    method: 'POST',
    data
  });
};

/**
 * 更新客户分组
 * @param id 分组ID
 * @param data 分组数据
 */
export const updateCustomerGroup = (id: string, data: Partial<CustomerGroup>) => {
  return request({
    url: `${API.CLASSES}/CustomerGroup/${id}`,
    method: 'PUT',
    data
  });
};

/**
 * 删除客户分组
 * @param id 分组ID
 */
export const deleteCustomerGroup = (id: string) => {
  return request({
    url: `${API.CLASSES}/CustomerGroup/${id}`,
    method: 'DELETE'
  });
};

/**
 * 获取分组内的客户列表
 * @param groupId 分组ID
 * @param params 查询参数
 */
export const getCustomersByGroup = (groupId: string, params?: any) => {
  return request({
    url: `${API.CLASSES}/CustomerGroupRelation`,
    method: 'GET',
    params: {
      where: JSON.stringify({ groupId }),
      include: 'customer',
      order: '-createdAt',
      limit: params?.limit || 10,
      skip: params?.skip || 0,
      count: 1,
      ...params
    }
  });
};

/**
 * 将客户添加到分组
 * @param groupId 分组ID
 * @param customerId 客户ID
 */
export const addCustomerToGroup = (groupId: string, customerId: string) => {
  return request({
    url: `${API.CLASSES}/CustomerGroupRelation`,
    method: 'POST',
    data: {
      groupId: {
        __type: 'Pointer',
        className: 'CustomerGroup',
        objectId: groupId
      },
      customer: {
        __type: 'Pointer',
        className: 'Customer',
        objectId: customerId
      }
    }
  });
};

/**
 * 将客户从分组中移除
 * @param relationId 关系记录ID
 */
export const removeCustomerFromGroup = (relationId: string) => {
  return request({
    url: `${API.CLASSES}/CustomerGroupRelation/${relationId}`,
    method: 'DELETE'
  });
};

export default {
  getMemberLevels,
  createMemberLevel,
  updateMemberLevel,
  deleteMemberLevel,
  getMemberLevelDetail,
  getCustomerList,
  createCustomer,
  updateCustomer,
  deleteCustomer,
  getCustomerGroups,
  createCustomerGroup,
  updateCustomerGroup,
  deleteCustomerGroup,
  getCustomersByGroup,
  addCustomerToGroup,
  removeCustomerFromGroup
}; 